package com.bawan.ddbb.service;

import com.bawan.ddbb.dao.LoginLogDao;
import com.bawan.ddbb.dao.UserDao;
import com.bawan.ddbb.domain.LoginLog;
import com.bawan.ddbb.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Desc:  User服务类
 * Author: zhengcongchun
 * CreateTime: 2014-05-28 13:57
 * LastModifyTime: 2014-05-28 13:57
 * FinalModifier: zhengcongchun
 * Version: 1.0
 */
@Service
public class UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private LoginLogDao loginLogDao;

    public boolean hashMatchUser(String username, String password) {
        int matchCount = userDao.getMatchCount(username, password);
        return matchCount > 0;
    }

    public void loginSuccess(User user) {
        user.setCredits(5 + user.getCredits());

        LoginLog log = new LoginLog();
        log.setUserId(user.getUserId());
        log.setVisitIp(user.getLastIp());
        log.setLoginDatetime(user.getLastVisit());

        userDao.updateLoginInfo(user);
        loginLogDao.insertLoginLog(log);
    }

    public User findUserByUsername(String username) {
        return userDao.findUserByUsername(username);
    }
}
